package leetcode_core.leetcode_3;

class ListNode{
    int val;
    ListNode next;
    ListNode(int val){
        this.val = val;
        this.next = null;
    }
}

public class MyLinkedList {
    private ListNode head;//头指针
    private ListNode tail;//尾指针
    private ListNode dummy;
    private int len = 0;
    public MyLinkedList() {
        dummy = new ListNode(-1);
        dummy.next = head;
        head.next = tail;
    }

    public int get(int index) {
        if(index>this.len){
            return -1;
        }
        ListNode pointer = head;
        for(int i = 0;i<index;i++){
            pointer = pointer.next;
        }
        return pointer.val;
    }

    public void addAtHead(int val) {
        //头插法
        ListNode newHead = new ListNode(val);
        dummy.next = newHead;
        newHead.next = head;
    }

    public void addAtTail(int val) {
        ListNode newTail = new ListNode(val);
        tail.next = newTail;
    }

    public void addAtIndex(int index, int val) {

    }

    public void deleteAtIndex(int index) {
        //定位 pre cur next
        if(index > len){//删除不了
            return ;
        }
        ListNode pre = head;
        for(int i=0;i<index-1;i++){
            pre = pre.next;
        }
        pre.next = pre.next.next;
    }
}
